/*
 * deviceManage.c
 *
 *  Created on: 2022.3.8
 *      Author: TracyHenry
 */
#include "deviceManage.h"

DeviceInfo *deviceList = NULL;
uint32_t numDevice = 0;


/******************************************************************************
 * FunctionName : addDevice
 * Description  : 添加新设备到链表
 * Parameters   : head 链表头部指针
 *                deviceId 设备id
 *                productId 产品id
 * Returns      : DeviceInfo 头部指针
*******************************************************************************/
DeviceInfo* addDevice(DeviceInfo *head, const char *deviceId, const char *productId) {

    if (deviceId==NULL || productId==NULL)
    {
        printf("deviceId or productId is null\n");
        return head;
    }
    
    DeviceInfo *newDevice = (DeviceInfo *)malloc(sizeof(DeviceInfo));
    if (!newDevice) {
        printf("ram malloc failed\n");
        return head;
    }

    strncpy(newDevice->deviceId, deviceId, 36);
    strncpy(newDevice->productId, productId, 37);
    newDevice->pNext = head;
    return newDevice;
}

/******************************************************************************
 * FunctionName : saveDeviceByDflash
 * Description  : 保存设备链表到Dflash
 * Parameters   : head 链表头部指针
 * Returns      : void
*******************************************************************************/
void saveDeviceByDflash(DeviceInfo *head) {

    if(head == NULL)
    {
        printf("saveDeviceByDflash failed\n");
        return;
    }
    
    spi_flash_write(START_SECTOR_DEVICE_INFO*0x1000,(uint32_t *)head,4096);
}
 
/******************************************************************************
 * FunctionName : deleteDevice
 * Description  : 删除指定deviceId的设备
 * Parameters   : head 链表头部指针
 *                deviceId 设备id
 * Returns      : DeviceInfo 头部指针
*******************************************************************************/
DeviceInfo* deleteDevice(DeviceInfo *head, const char *deviceId) {
    DeviceInfo *current = head;
    DeviceInfo *prev = NULL;
 
    while (current != NULL) {
        if (strcmp(current->deviceId, deviceId) == 0) {
            if (prev == NULL) {
                // 删除头节点
                current = current->pNext;
            } else {
                prev->pNext = current->pNext;
            }
            free(current);
            printf("device %s is deleted\n", deviceId);
            return current;
        }
        prev = current;
        current = current->pNext;
    }
    printf("no found device %s\n", deviceId);
    return current;
}
 
/******************************************************************************
 * FunctionName : deleteDevice
 * Description  : 修改设备信息
 * Parameters   : head 链表头部指针
 *                deviceId 设备id
 *                productId 产品id
 *                newDeviceStatus 设备状态
 * Returns      : 0 成功，1 失败
*******************************************************************************/
int updateDeviceStatus(DeviceInfo *head, const char *deviceId, const char *productId, const uint8_t newDeviceStatus) {
    DeviceInfo *current = head;
    while (current != NULL) {
        if (strcmp(current->deviceId, deviceId) == 0) {
            if (productId) {
                strncpy(current->productId, productId, 32);
            }
            current->deviceStatus = newDeviceStatus;
            printf("device %s updated\n", deviceId);
            return 1;
        }
        current = current->pNext;
    }
    printf("no found device %s\n", deviceId);
    return 0;
}
 

/******************************************************************************
 * FunctionName : findDevice
 * Description  : 查找设备
 * Parameters   : head 链表头部指针
 *                deviceId 设备id
 * Returns      : DeviceInfo 头部指针
*******************************************************************************/
DeviceInfo* findDevice(DeviceInfo *head, const char *deviceId) {
    DeviceInfo *current = head;
    while (current != NULL) {
        if (strcmp(current->deviceId, deviceId) == 0) {
            return current;
        }
        current = current->pNext;
    }
    return NULL;
}

// 打印链表中的所有设备
void printDevices(DeviceInfo *head) {
    while (head != NULL) {
        printf("deviceId: %s, type: %s, status: %d\n", head->deviceId, head->productId, head->deviceStatus);
        head = head->pNext;
    }
}
 
// 释放链表内存
void freeDevices(DeviceInfo *head) {
    while (head != NULL) {
        DeviceInfo *temp = head;
        head = head->pNext;
        free(temp);
    }
}

//system_param_save_with_protect 写扇区，
//system_param_load 读扇区。这两个函数得深入理解，是否需要替换使用spi_flash_read和spi_flash_write
uint8_t initDeviceNumInfo() 
{
    uint32_t numbuff;
    spi_flash_read(START_SECTOR_DEVICE_NUMBER_INFO*0x01000,&numbuff, 4);
    if(numbuff > 20)
    {
        if(numbuff==0xFFFFFFFF)
        {
            printf("numDevice is empty\n");
            numDevice = 0;
            return NO_ERR;
        }
        else
        {
            numDevice = 255;
            printf("numDevice is too big\n");
            return ERR;
        }
    }
    else
    {
        numDevice = numbuff;
        deviceList = (DeviceInfo *)malloc(sizeof(DeviceInfo)*numDevice);
        spi_flash_read(START_SECTOR_DEVICE_INFO*0x1000,(uint32_t *)deviceList, sizeof(DeviceInfo)*numDevice);
        printf("numDevice is %d\n,",numDevice);
        return NO_ERR;
    }
}